﻿using System;
using System.Windows.Forms;
using NanoERP.Model;
using NanoERP.Business.BaseInfo;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    //[Module("FrmDicDataEdit")]
    public partial class FrmDicDataEdit : Form
    {
        Sysdictable _sysdictable;
        readonly SysdictableBLL _sysdictablebLl;
        public FrmDicDataEdit()
        {
            InitializeComponent();
        }

        public FrmDicDataEdit(Sysdictable sysdictable, string parentName)
        {
            InitializeComponent();

            _sysdictablebLl = new SysdictableBLL();
            _sysdictable = sysdictable;
            txtParent.Text = parentName;

            BindFormControl();
        }

        void BindFormControl()
        {

            txtDicname.DataBindings.Add(new Binding("Text", _sysdictable, "Dicname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtDiccode.DataBindings.Add(new Binding("Text", _sysdictable, "Diccode", true, DataSourceUpdateMode.OnPropertyChanged));
            txtDescription.DataBindings.Add(new Binding("Text", _sysdictable, "Description", true, DataSourceUpdateMode.OnPropertyChanged));
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtDicname.Text.Length == 0)
            {
                MessageBox.Show(@"类别名称不能为空，请输入.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDicname.Focus();
                return;
            }

            if (txtDiccode.Text.Length == 0)
            {
                MessageBox.Show(@"类别编码不能为空，请输入.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDiccode.Focus();
                return;
            }

            _sysdictablebLl.Edit(_sysdictable);

            MessageBox.Show(@"保存成功.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (!chIsAdd.Checked)
            {
                this.DialogResult = DialogResult.OK;
                return;
            }
            ControlUtil.ClearControlBind(panel1);
            int id = _sysdictable.Parent;
            _sysdictable = new Sysdictable { Parent = id };
            BindFormControl();
        }
    }
}
